#include "comm.hpp"

int main()
{   
    int shmid = GetShm();
    if(shmid == -1)
    {
        std::cout << "Error creating shared memory" << std::endl;
        return 1;
    }

    char* shmaddr = (char*)shmat(shmid, nullptr, 0);

    int fd = open(FIFO_FILE, O_WRONLY);     // 等待写入方打开之后，自己才会打开文件，向后执行， open 阻塞了！
    if(fd == -1)
    {
        log(Fatal, "error string: %s, error code: %d", strerror(errno), errno);
        exit(FIFO_OPEN_ERR);
    }


    while (true)
    {
        cout << "Enter message: ";
        fgets(shmaddr, 4096, stdin);
        
        write(fd, "1", 1);      //先往SHM中写入数据，再通知写入方读取数据
    }

    shmdt(shmaddr);     // 解除共享内存的映射(取消挂接)
    close(fd);

    return 0;
}